package com.hotline.monitor.service.monitor;

import com.alibaba.fastjson.JSON;
import com.hotline.monitor.dto.*;
import com.hotline.monitor.util.HttpUtils;
import com.hotline.monitor.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MonitorServiceImpl implements MonitorService{

    private static final String URI_SET_AGENT_STATUS = "/monitor/set-agent-status";

    private static final String URI_SET_SKILL_QUEUE_NUM = "/monitor/set-skill-queue-num";

    private static final String URI_SET_QUEUE_SKILL_WAIT_NUM = "/monitor/set-queue-skill-wait-num";

    private static final String URI_SET_ORG_WAIT_NUM = "/monitor/set-org-wait-num";

    private static final String URI_SET_ORG_WAIT_NUM_12366 = "/monitor/set-org-wait-num-12366";

    private static final String URI_SET_ORG_WAIT_NUM_12345 = "/monitor/set-org-wait-num-12345";

    @Value("${org.id}")
    private String orgIds;

    @Value("${agent.worknumber}")
    private String workNumber;

    @Value("${hotline.url}")
    private String url;
    /**
     * 设置坐席状态
     *
     * @param agentStatusList
     */
    @Override
    public void setAgentStatus(List<AgentStatusDTO> agentStatusList) {
        setAgentStatus(agentStatusList,orgIds);
    }

    /**
     * 设置坐席状态
     *
     * @param agentStatusList
     */
    @Override
    public void setAgentStatus(List<AgentStatusDTO> agentStatusList,String orgId) {
        if(StringUtils.isNotBlank(orgId) && orgId.length() > 4){
            String reqOrgId = orgId.substring(0,4);
            OrgAgentInfoDTO orgAgentInfoDTO = getAgentInfoList(agentStatusList,reqOrgId);
            String reqUrl = url + URI_SET_AGENT_STATUS;
            String resultString = HttpUtils.doPostJson(reqUrl, JSON.toJSONString(orgAgentInfoDTO));
            log.info("设置坐席状态请求返回：{}",resultString);
        }
    }



    /**
     * 设置队列排队数
     *
     * @param waitNum
     */
    @Override
    public void setSkillQueueNum(Integer waitNum) {
        setSkillQueueNum(orgIds,waitNum);
    }


    /**
     * 设置队列排队数
     *
     * @param waitNum
     */
    public void setSkillQueueNum(String orgId,Integer waitNum) {
        String reqUrl = url + URI_SET_SKILL_QUEUE_NUM;

        Map param = new HashMap<>();

        param.put("orgId",orgId);
        param.put("waitNum",String.valueOf(waitNum));

        String resultString = HttpUtils.doPost(reqUrl, param);

        log.debug("设置机构排队数请求返回：{}",resultString);
    }


    @Override
    public void setOrgWaitNum(List<SkillWaitNumRequestDTO> skillWaitNumList) {
        if(!CollectionUtils.isEmpty(skillWaitNumList)){
            List<OrgWaitNumDTO> orgWaitNumList = getOrgWaitNumList(skillWaitNumList);
            String reqUrl = url + URI_SET_ORG_WAIT_NUM;
            String resultString = HttpUtils.doPostJson(reqUrl, JSON.toJSONString(orgWaitNumList));
            log.info("设置技能队列排队数：{}",resultString);
        }
    }

    @Override
    public void setOrgWaitNum12366(List<SkillWaitNumRequestDTO> skillWaitNumList) {
        if(!CollectionUtils.isEmpty(skillWaitNumList)){
            List<OrgWaitNumDTO> orgWaitNumList = getOrgWaitNumList(skillWaitNumList);
            String reqUrl = url + URI_SET_ORG_WAIT_NUM_12366;
            String resultString = HttpUtils.doPostJson(reqUrl, JSON.toJSONString(orgWaitNumList));
            log.info("设置技能队列排队数：{}",resultString);
        }
    }

    @Override
    public void setOrgWaitNum12345(List<SkillWaitNumRequestDTO> skillWaitNumList) {
        if(!CollectionUtils.isEmpty(skillWaitNumList)){
            List<OrgWaitNumDTO> orgWaitNumList = getOrgWaitNumList(skillWaitNumList);
            String reqUrl = url + URI_SET_ORG_WAIT_NUM_12345;
            String resultString = HttpUtils.doPostJson(reqUrl, JSON.toJSONString(orgWaitNumList));
            log.info("设置技能队列排队数：{}",resultString);
        }
    }

    @Override
    public void setQueueSkillWaitNum(List<SkillWaitNumRequestDTO> skillWaitNumList) {
        if(!CollectionUtils.isEmpty(skillWaitNumList)){
            String reqUrl = url + URI_SET_QUEUE_SKILL_WAIT_NUM;
            String resultString = HttpUtils.doPostJson(reqUrl, JSON.toJSONString(skillWaitNumList));
            log.info("设置技能队列排队数：{}",resultString);
        }
    }

    /**
     * 坐席信息
     * @param agentStatusList
     * @param reqOrgId
     * @return
     */
    private OrgAgentInfoDTO getAgentInfoList(List<AgentStatusDTO> agentStatusList, String reqOrgId){
        OrgAgentInfoDTO orgAgentInfoDTO = new OrgAgentInfoDTO();
        List<AgentInfoDTO> agentInfoList = new ArrayList<>();
        if(Objects.nonNull(agentStatusList) && !agentStatusList.isEmpty()){
            for(AgentStatusDTO agentStatusDTO : agentStatusList){
                AgentInfoDTO agentInfoDTO = new AgentInfoDTO();
                agentInfoDTO.setReqOrgId(reqOrgId);
                agentInfoDTO.setWorkNumber(agentStatusDTO.getWorkno());
                if(StringUtils.isNotBlank(agentStatusDTO.getStatus())){
                    agentInfoDTO.setStatus(new Integer(agentStatusDTO.getStatus()));
                }
                agentInfoDTO.setExtensionNum(agentStatusDTO.getPhonenumber());
                agentInfoDTO.setCurrentStateTime(agentStatusDTO.getCurrentstatetime());
                agentInfoDTO.setCurrentState(0);
                //监控坐席不显示在大屏上
                agentInfoList.add(agentInfoDTO);
            }
        }
        orgAgentInfoDTO.setReqOrgId(reqOrgId);
        orgAgentInfoDTO.setAgentInfoList(agentInfoList);
        return orgAgentInfoDTO;
    }


    /**
     * 获取机构排队数请求对象
     * @param skillWaitNumList
     * @return
     */
    private List<OrgWaitNumDTO> getOrgWaitNumList(List<SkillWaitNumRequestDTO> skillWaitNumList){
        //根据机构分组
        Map<String, List<SkillWaitNumRequestDTO>> skillWaitNumGroup =
                skillWaitNumList.stream().collect(Collectors.groupingBy(SkillWaitNumRequestDTO::getOrgId));

        List<OrgWaitNumDTO> skillQueueList = new ArrayList<>();

        if(Objects.nonNull(skillWaitNumGroup)){
            skillWaitNumGroup.forEach((orgId,skillWaitNumRequestList)->{
                Integer waitNum = 0;
                for(SkillWaitNumRequestDTO skillWaitNumRequestDTO : skillWaitNumRequestList){
                    waitNum += skillWaitNumRequestDTO.getWaitNum();
                }
                OrgWaitNumDTO orgWaitNumDTO = new OrgWaitNumDTO();
                orgWaitNumDTO.setOrgId(orgId);
                orgWaitNumDTO.setWaitNum(waitNum);
                skillQueueList.add(orgWaitNumDTO);
            });
        }
        return skillQueueList;
    }
}
